var app = getApp();

Page({
  data: {
    codeBtnText: '发送验证码',
    // region: ['北京市', '北京市', '东城区'],
    selectedIds: [65112, 65113, 65114],
    multiArray: [],
    multiIndex: [0, 0],
    multiArrayOne: [],
    multiArrayTwo: [],
    multiArrayIdOne: [],
    multiArrayIdTwo: [],
    tipwords: '',
    createTime: '',
    morningOrAfter: 0,
    telephone: 0,
    sec: 60
  },
  onLoad: function (options) {
    var that = this;
    this.getTipsWord(); // 获取提示语
    // 获取预约时间
    this.setData({
      createTime: options.date,
      morningOrAfter: options.morningOrAfter
    });
    // 获取区县
    wx.request({
      url: app.globalData.apiBaseUrl + '/city/getArea',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      data: { id: 65112 },
      dataType: 'json',
      success: function (res) {
        if (res.data.code == 200) {
          var districtOneArr = res.data.data
          var multiArrayOne = districtOneArr.map((item, index, arr) => {
            return item.name
          })
          var multiArrayIdOne = districtOneArr.map((item, index, arr) => {
            return item.id
          })
          that.setData({
            multiArrayOne: multiArrayOne,
            multiArrayIdOne: multiArrayIdOne
          })
          that.getTwo(multiArrayIdOne[0]);
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
          setTimeout(function () {
            wx.hideToast()
          }, 3000);
        }
      }
    })
  },
  bindMultiPickerChange: function (e) {
    var that = this;
    var idsOne = this.data.multiArrayIdOne[this.data.multiIndex[0]];
    var idsTwo = this.data.multiArrayIdTwo[this.data.multiIndex[1]];
    this.setData({
      multiIndex: e.detail.value,
      selectedIds: [65112, idsOne, idsTwo]
    })
    // console.log(this.data.selectedIds)
  },
  bindMultiPickerColumnChange: function (e) {
    var that = this;
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex,
      multiArrayIdTwo: this.data.multiArrayIdTwo
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    var id_session = this.data.multiArrayIdOne[0];
    switch (e.detail.column) {
      case 0:
        var multiArrayIdOne = this.data.multiArrayIdOne;
        var paramsId = multiArrayIdOne[e.detail.value];
        that.getTwo(paramsId);
        data.multiIndex[1] = 0;
        break;
    }
    this.setData(data);
  },
  getTwo: function (parentId) {
    var that = this;
    wx.request({
      url: app.globalData.apiBaseUrl + '/city/getArea',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      data: { id: parentId },
      dataType: 'json',
      success: function (res) {
        if (res.data.code == 200) {
          var districtTwoArr = res.data.data
          var multiArrayTwo = districtTwoArr.map((item, index, arr) => {
            return item.name
          })
          var multiArrayIdTwo = districtTwoArr.map((item, index, arr) => {
            return item.id
          })
          that.setData({
            multiArrayTwo: multiArrayTwo,
            multiArrayIdTwo: multiArrayIdTwo
          })
          var arrOne = that.data.multiArrayOne;
          var arrTwo = that.data.multiArrayTwo;
          that.setData({
            multiArray: [arrOne, arrTwo]
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          });
          setTimeout(function () {
            wx.hideToast()
          }, 3000);
        }
      }
    })
  },
  getPhoneNumber: function(event) {
    var val = event.detail.value;
    this.setData({
      telephone: val
    });
  },
  // 发送验证码
  sendPhoneCode: function(event) {
    var that = this;
    var thisVal = event._relatedInfo.anchorTargetText;
    var myreg = /^(14[0-9]|13[0-9]|15[0-9]|17[0-9]|18[0-9])\d{8}$$/;
    if (myreg.test(this.data.telephone)) {
      if (thisVal == '发送验证码') {
        var remain = 60; //用另外一个变量来操作秒数是为了保存最初定义的倒计时秒数，就不用在计时完之后再手动设置秒数
        var time = setInterval(function () {
          if (remain == 1) {
            clearInterval(time);
            that.setData({
              sec: 60,
              codeBtnText: '发送验证码'
            })
            return false //必须有
          }
          remain--;
          that.setData({
            sec: remain,
            codeBtnText: that.data.sec + 's重新获取'
          })
        }, 1000);

        wx.request({
          url: app.globalData.apiBaseUrl + '/wechat/sendMessage',
          header: {
            'content-type': 'application/json; charset=utf-8'
          },
          method: 'GET',
          data: {
            telephone: that.data.telephone
          },
          dataType: 'json',
          success: function (msg) {
            console.log(msg);
          }
        })
      }
    } else {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none'
      })
    }
    setTimeout(function () {
      wx.hideToast()
    }, 3000)
  },
  // bindRegionChange(e) {
  //   this.setData({
  //     region: e.detail.value
  //   })
  // },
  // 获取提示语
  getTipsWord: function () {
    var that = this;
    wx.request({
      url: app.globalData.apiBaseUrl + '/homePageSetting/getSystemSetting',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        if (res.data.code == 200) {
          that.setData({
            tipwords: res.data.data.infoPageNote
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
    setTimeout(function () {
      wx.hideToast()
    }, 3000)
  },
  // 提交表单
  formSubmit(e) {
    var that = this;
    var userId = wx.getStorageSync('openid');
    var formObj = e.detail.value;
    formObj.province = this.data.selectedIds[0].toString();
    formObj.ciyt = this.data.selectedIds[1].toString();
    formObj.area = this.data.selectedIds[2].toString();
    formObj.createTime = this.data.createTime;
    formObj.morningOrAfter = this.data.morningOrAfter;
    formObj.userId = userId;
    // console.log(formObj);
    var sameOrderParams = {
      userId: userId,
      createTime: this.data.createTime
    };
    wx.request({
      url: app.globalData.apiBaseUrl + '/studentOrder/getSameOrder',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      data: sameOrderParams,
      dataType: 'json',
      success: function (res) {
        if (res.data.code == 200) {
          // console.log(res.data.data);
          if (formObj.applyName != '' && formObj.detailAddress != '' && formObj.hightSchole != '' && formObj.idCard != '' && formObj.sex != '' && formObj.studyCollege != '' && formObj.telephone != '' && formObj.varifyCode != '') {
            wx.request({
              url: app.globalData.apiBaseUrl + '/studentOrder/addStudentOrder',
              header: {
                'content-type': 'application/json; charset=utf-8'
              },
              method: 'POST',
              data: formObj,
              dataType: 'json',
              success: function (f) {
                if (f.data.code == 200) {
                  wx.request({
                    url: app.globalData.apiBaseUrl + '/studentOrder/userOrderDetail',
                    header: {
                      'content-type': 'application/json; charset=utf-8'
                    },
                    method: 'POST',
                    data: f.data.data,
                    dataType: 'json',
                    success: function (msg) {
                      if (msg.data.code == 200) {
                        wx.showToast({
                          title: '预约成功',
                          icon: 'success'
                        });
                        // 跳转预约页面
                        wx.redirectTo({
                          url: "../query/query"
                        });
                      } else {
                        wx.showToast({
                          title: msg.data.error,
                          icon: 'none'
                        })
                      }
                    }
                  })
                } else {
                  wx.showToast({
                    title: f.data.msg,
                    icon: 'none'
                  })
                }
              }
            })
          } else {
            wx.showToast({
              title: '请完善填写信息',
              icon: 'none'
            })
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
    setTimeout(function() {
      wx.hideToast()
    }, 3000)
  }
})